package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.enums.FeeBackEnum;
import com.settlement.system.common.enums.UserReadEnum;
import com.settlement.system.mapper.SysFeedbackMapper;
import com.settlement.system.mapper.SysLazyToolUserMapper;
import com.settlement.system.model.entity.SysFeedback;
import com.settlement.system.model.entity.SysLazyToolUser;
import com.settlement.system.model.form.SysFeedbackForm;
import com.settlement.system.model.query.SysFeedbackQuery;
import com.settlement.system.model.vo.SysFeedbackImportVo;
import com.settlement.system.service.SysFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户反馈表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-12-28
 */
@Service
@RequiredArgsConstructor
public class SysFeedbackServiceImpl extends ServiceImpl<SysFeedbackMapper, SysFeedback> implements SysFeedbackService {


    private final SysLazyToolUserMapper sysLazyToolUserMapper;

    @Override
    public Page<SysFeedback> getPage(SysFeedbackQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysFeedback> page = new Page<>(pageNum, pageSize);
        Page<SysFeedback> feedbackPage = this.baseMapper.getPage(page, queryParams);
        if (feedbackPage.getRecords().size() > 0) {
            for (SysFeedback record : feedbackPage.getRecords()) {
                if (FeeBackEnum.N.getValue().equals(record.getStatus())) {
                    record.setStatusName(FeeBackEnum.N.getLabel());
                } else {
                    record.setStatusName(FeeBackEnum.Y.getLabel());
                }
                if (UserReadEnum.N.getValue().equals(record.getUserRead())) {
                    record.setUserReadName(UserReadEnum.N.getLabel());
                } else {
                    record.setUserReadName(UserReadEnum.Y.getLabel());
                }
            }
        }
        return feedbackPage;
    }

    @Override
    public boolean saveSysFeedback(SysFeedbackForm sysFeedbackForm) {
        SysFeedback sysFeedback = new SysFeedback();
        BeanUtil.copyProperties(sysFeedbackForm, sysFeedback);
        sysFeedback.setCreateTime(null);
        return this.saveOrUpdate(sysFeedback);
    }

    @Override
    public boolean saveSysFeedback(SysFeedback sysFeedback) {
        SysLazyToolUser sysLazyToolUser = sysLazyToolUserMapper.selectById(sysFeedback.getUserId());
        sysFeedback.setMobile(sysLazyToolUser.getMobile());
        return this.saveOrUpdate(sysFeedback);
    }

    @Override
    public SysFeedbackForm getSysFeedbackFormData(Long id) {
        SysFeedback sysFeedback = this.getById(id);
        SysFeedbackForm sysFeedbackForm = new SysFeedbackForm();
        BeanUtil.copyProperties(sysFeedback, sysFeedbackForm);
        return sysFeedbackForm;
    }

    @Override
    public boolean deleteSysFeedback(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysFeedbackImportVo> voList) {
        List<SysFeedback> sysFeedbackList = new ArrayList<>();
        for (SysFeedbackImportVo importVo : voList) {
            SysFeedback sysFeedback = new SysFeedback();
            BeanUtil.copyProperties(importVo, sysFeedback);
            sysFeedbackList.add(sysFeedback);
        }
        this.saveOrUpdateBatch(sysFeedbackList);
    }

    @Override
    public List<SysFeedbackImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysFeedback> sysFeedbackList = this.getBaseMapper().selectBatchIds(ids);
        List<SysFeedbackImportVo> voList = new ArrayList<>();
        for (SysFeedback sysFeedback : sysFeedbackList) {
            SysFeedbackImportVo vo = new SysFeedbackImportVo();
            BeanUtil.copyProperties(sysFeedback, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public boolean updateResult(SysFeedbackForm sysFeedbackForm) {
        SysFeedback sysFeedback = this.baseMapper.selectById(sysFeedbackForm.getId());
        // 更新结果
        sysFeedback.setResult(sysFeedbackForm.getResult());
        sysFeedback.setStatus(FeeBackEnum.Y.getValue());
        return this.updateById(sysFeedback);
    }

    @Override
    public void readFeeBack(Long feeBackId) {
        SysFeedback sysFeedback = this.baseMapper.selectById(feeBackId);
        // 改成用户已读
        sysFeedback.setUserRead(UserReadEnum.Y.getValue());
        this.updateById(sysFeedback);
    }

    @Override
    public List<SysFeedback> feeBackList(Long userId, Integer pageNumber) {
        SysFeedbackQuery queryParams = new SysFeedbackQuery();
        queryParams.setPageSize(10);
        queryParams.setPageNum(pageNumber);
        queryParams.setUserId(userId);
        Page<SysFeedback> feedbackPage = this.getPage(queryParams);
        for (SysFeedback record : feedbackPage.getRecords()) {
            if(FeeBackEnum.N.getValue().equals(record.getStatus())){
                record.setStatusName(FeeBackEnum.N.getLabel());
            }else{
                record.setStatusName(FeeBackEnum.Y.getLabel());
            }
        }
        return feedbackPage.getRecords();
    }
}
